package com.gxa.modules.pc.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxa.common.utils.DateUtils;
import com.gxa.common.utils.Query;
import com.gxa.common.utils.Result;
import com.gxa.common.utils.TPage;
import com.gxa.modules.model.dto.DepartmentDTO;
import com.gxa.modules.model.dto.DoctorDTO;
import com.gxa.modules.model.dto.ParkDTO;
import com.gxa.modules.model.entity.book.BookRegister;
import com.gxa.modules.model.entity.hospital.Department;
import com.gxa.modules.model.vo.ScheduleVO;
import com.gxa.modules.model.dto.WaitDTO;
import com.gxa.modules.model.vo.WaitVO;
import com.gxa.modules.pc.hospital.mapper.DepartmentMapper;
import com.gxa.modules.pc.hospital.mapper.HospitalParkMapper;
import com.gxa.modules.pc.hospital.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.modules.pc.order.mapper.BookRegisterMapper;
import com.gxa.modules.pc.order.mapper.ScheduleMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author zhuiyi
 * @since 2022-11-13 05:39:52
 */
@Service
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private HospitalParkMapper hospitalParkMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private BookRegisterMapper bookRegisterMapper;

    @Override
    public TPage queryByPage(Map<String, Object> searchMap) {
        //其他的查询参数
        String departmentId = (String) searchMap.get("departmentId");
        String doctorName = (String) searchMap.get("doctorName");

        String orderField = (String) searchMap.get("orderField");
        String order = (String) searchMap.get("order");

        log.info("------departmentId----{}--doctorName---{}------", departmentId, doctorName);


        IPage<Department> oldPage = this.page(new Query<Department>().getPage(searchMap), new QueryWrapper<Department>()
                .eq(!StringUtils.isBlank(departmentId), "department_id", departmentId)
                .likeRight(!StringUtils.isBlank(doctorName), "doctor_name", doctorName)
        );
        return new TPage(oldPage);
    }

    @Override
    public boolean saveOne(DepartmentDTO dto) {
        Department department = new Department();
        department.setDepartmentName(dto.getDepartmentName());
        department.setDepAddress(dto.getDepAddress());
        department.setBigName(dto.getBigName());
        department.setStauts(dto.getStauts());
        department.setHoppitalParkId(dto.getHoppitalParkId());
        department.setHospitalParkName(dto.getHospitalParkName());
        if (save(department)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateOne(DepartmentDTO dto) {
        //查询
        Department department = this.getById(dto.getDepartmentId());
        //判断
        if (department == null) {
            return false;
        }
        //更新
        department.setDepartmentName(dto.getDepartmentName());
        department.setDepAddress(dto.getDepAddress());
        department.setBigName(dto.getBigName());
        department.setStauts(dto.getStauts());
        department.setHoppitalParkId(dto.getHoppitalParkId());
        department.setHospitalParkName(dto.getDepartmentName());
        if (updateById(department)) {
            return true;
        }
        return false;
    }

    @Override
    public Set<String> getBigNames() {
        Set<String> bigNames = this.departmentMapper.queryBigNames();
        return bigNames;
    }

    public List<Department> getDeparments(Integer partId, String bigName) {
        return this.baseMapper.selectList(new QueryWrapper<Department>()
                .eq("hoppital_park_id", partId)
                .eq("big_name", bigName)
        );
    }

    @Override
    public Result getDoctors(String departmentName, String day) {
        List<DoctorDTO> doctors = this.departmentMapper.getDoctors(departmentName, day);
        Map<String, Object> map = new HashMap<>();
        map.put("doctors", doctors);
        return new Result().ok(map);
    }

    @Override
    public List<ParkDTO> getPartList() {
        List<ParkDTO> parkList = this.hospitalParkMapper.getParkList();
        return parkList;
    }

    @Override
    public List<ScheduleVO> getDoctorSchedule(String doctorId) {
        String today = DateUtils.getStringToday();
        List<ScheduleVO> schedules = this.scheduleMapper.getDoctorSchedule(doctorId,today );
        for (ScheduleVO vo : schedules) {
            String workDate = vo.getWorkDate();
            vo.setWeek(DateUtils.getDayWithWeekString(DateUtils.stringToDate(vo.getWorkDate(),"yyyy-MM-dd")));
            int diff = DateUtils.getDaysBetween(today, workDate);
            vo.setDiff(Integer.toString(diff));
        }
        return schedules;
    }

    @Override
    public List<WaitVO> getWaitFromDoctor(Integer userId) {
        List<WaitDTO> waitDTOList = departmentMapper.getWaitFromDoctor(userId);
        List<WaitVO> waitVOList = new ArrayList<>();
        WaitVO vo = null;
        for(WaitDTO dto:waitDTOList){
            vo = new WaitVO();
            vo.setDoctorName(dto.getDoctorName());
            vo.setAddress(dto.getDepartmentName() + "-" + dto.getDepAddress());
            vo.setNum(dto.getRegisterId());
            vo.setPersonNum(dto.getSortRandom());
            waitVOList.add(vo);
        }
        return waitVOList;
    }

    @Override
    public int randomUpdate(Integer appointId) {
        Random r = new Random();
        int num1 = r.nextInt(10)+1;
        if(num1>5){
            BookRegister bookRegister = bookRegisterMapper.selectById(appointId);
            if( bookRegister != null && bookRegister.getSortRandom()>0){
                bookRegister.setSortRandom(bookRegister.getSortRandom()-1);
                bookRegisterMapper.updateById(bookRegister);
            }
        }

        return bookRegisterMapper.selectById(appointId).getSortRandom();
    }
}
